{
sort $isid_vars
capture drop sample
gen sample=(S_pds==1 & n_ud==1) 	

* Seller-buyer-product-year markup factors (as a weighted average)
local L  sbjt  
foreach l of local L{

capture drop xx
gen xx= mu_o_B*exp(ln_q_sbjo) if n_ud==1 & sample==1 

capture drop yy
gen yy=exp(ln_q_sbjo) if n_ud==1 & sample==1 
capture drop QQ
sort gr_`l' $isid_vars
bys gr_`l': egen QQ=total(yy)
replace xx=xx/QQ

capture drop  mu_`l'
sort gr_`l' $isid_vars
bys gr_`l': egen mu_`l'=total(xx)
replace mu_`l'=. if mu_`l'==0

capture drop ln_mu_`l'
gen ln_mu_`l'=ln(mu_`l')		
}

* Combinations in sample
sort $isid_vars
capture drop sbjt_in_sample
bys gr_sbjt: egen sbjt_in_sample=max(sample) 
capture drop sample
gen sample=(sbjt_in_sample==1 & n_sbjt==1)
capture drop s_in_sample
bys s_id: egen s_in_sample=max(sample) 
sort $isid_vars
capture drop n_s
bys s_id: gen n_s=_n

* Residualize markups on product-year fixed effects
capture drop residual_mu
reghdfe  ln_mu_sbjt if sample==1, abs(gr_jt, savefe) resid
predict residual_mu, resid

* Average residualized markup for the seller (across sbjt combinations)
capture drop xx
gen xx=residual_mu if sample==1
bys s_id: egen av_resid_mu=mean(xx)
capture drop yy
bys s_id: egen yy=median(av_resid_mu) 
replace av_resid_mu=yy
* Median residualized markup for the seller (across sbjt combinations)
bys s_id: egen med_resid_mu=median(xx)
capture drop yy
bys s_id: egen yy=median(med_resid_mu) 
replace med_resid_mu=yy
* 75th percentile residualized markup for the seller (across sbjt combinations)
bys s_id: egen p75_resid_mu=pctile(xx), p(75)			
capture drop yy
bys s_id: egen yy=median(p75_resid_mu) 
replace p75_resid_mu=yy 
* 25th percentile residualized markup for the seller (across sbjt combinations)
bys s_id: egen p25_resid_mu=pctile(xx), p(25)           
capture drop yy
bys s_id: egen yy=median(p25_resid_mu) 
replace p25_resid_mu=yy 
* Count of datapoints being used 
bys s_id: egen num_resid_mu=nvals(xx)
capture drop yy
bys s_id: egen yy=median(num_resid_mu) 
replace num_resid_mu=yy

* Sellers with sufficient datapoiints: 
sum num_resid_mu if s_in_sample==1 & n_s==1, d
gen plot=(n_s==1 & s_in_sample==1 & num_resid_mu>=10)
* Discarding approx 10% of the sellers

* Order sellers according to their average markup (smallest to largest)
capture drop xx
gen xx=av_resid_mu if plot==1
egen pctile_s_mu=xtile(xx), n(100)
capture drop yy
bys s_id: egen yy=median(pctile_s_mu)
replace pctile_s_mu=yy 

* Generating bins to group sellers according to average residual markup 
capture drop bin n_bin bin_in_sample av_p75_ av_p25 av_av_bin  
gen bin=.
forvalues i=0(1)30{
replace bin=5*(`i'+1) if pctile_s_mu>`i'*5 & pctile_s_mu<=(`i'+1)*5
} 
* 

* Average, p75 and p25 residual markups for seller inside the bin
capture drop xx
gen xx=av_resid_mu if  plot==1
bys bin: egen av_av_bin=mean(xx)
capture drop yy
bys bin: egen yy=median(av_av_bin) 
replace av_av_bin=yy

capture drop xx
gen xx=p75_resid_mu if  plot==1
bys bin: egen av_p75_bin=mean(xx)
capture drop yy
bys bin: egen yy=median(av_p75_bin) 
replace av_p75_bin=yy

capture drop xx
gen xx=p25_resid_mu if  plot==1
bys bin: egen av_p25_bin=mean(xx)
capture drop yy
bys bin: egen yy=median(av_p25_bin) 
replace av_p25_bin=yy


bys bin: gen n_bin=_n
bys bin: egen bin_in_sample=max(plot)

sort pctile_s_mu
twoway scatter av_av_bin bin if n_bin==1 & bin_in_sample==1, connect(ascending) sort(pctile_s_mu) msymbol(none) lcolor(black) yaxis(1) || ///
scatter av_p75_bin bin if n_bin==1 & bin_in_sample==1,  sort(pctile_s_mu)  msymbol(x) mcolor(black) connect(ascending) lpattern(dot) lcolor(black)   || ///
scatter av_p25_bin bin if n_bin==1 & bin_in_sample==1,  sort(pctile_s_mu)  msymbol(x) mcolor(black) connect(ascending) lpattern(dot) lcolor(black)   ///
 xtitle("Percentile Seller (Average Residual Markup)", size(small)) ytitle("Log Residual Markup (p25,mean,p75)" , axis(1) size(small))   ///
 graphregion(color(white)) legend(off) ylab(, nogrid)  xlab(0(10)100)  yline(0.17583605 -0.18096445 , lpattern(dash) lcolor(black))
graph export "${figures}\FigureE1.pdf", replace   

capture drop QQ mu_sbjt  sample sbjt_in_sample  s_in_sample n_s ///
 __hdfe1__residual_mu av_resid_mu med_resid_mu p75_resid_mu p25_resid_mu ///
 num_resid_mu plot pctile_s_mu iqr_s_mu bin av_av_bin av_p75_bin xx ///
 av_p25_bin yy n_bin bin_in_sample
}
